package com

import com.XferMgrProgress.waitForCompletion
import com.amazonaws.AmazonServiceException
import com.amazonaws.services.s3.model.CannedAccessControlList
import com.amazonaws.services.s3.model.ObjectMetadata
import com.amazonaws.services.s3.model.PutObjectRequest
import com.amazonaws.services.s3.transfer.Transfer.TransferState
import com.amazonaws.services.s3.transfer.TransferManager
import com.amazonaws.services.s3.transfer.Upload
import com.interfaces.UploadCallback
import java.io.InputStream

class UploadFile(
    private var transferManager: TransferManager? = null,
    private var bucketName: String? = null,
    val filePath: String? = null,
    val inputStream: InputStream? = null,
    private var meta: ObjectMetadata? = null
) {

    private val por: PutObjectRequest by lazy {
        PutObjectRequest(bucketName, filePath, inputStream, meta)
    }
    private var callback: UploadCallback? = null
    private var key: String? = null

    /**
     * 开始上传
     */
    fun upload() {
        try {
            // 设置此文件为公共读取
            por.withCannedAcl(CannedAccessControlList.PublicRead)


            //            void setObjectAcl(String var1, String var2, AccessControlList var3) throws SdkClientException, AmazonServiceException;
//
//            void setObjectAcl(String var1, String var2, String var3, AccessControlList var4) throws SdkClientException, AmazonServiceException;
//
//            void setObjectAcl(String var1, String var2, String var3, CannedAccessControlList var4) throws SdkClientException, AmazonServiceException;
//
//            void setObjectAcl(SetObjectAclRequest var1) throws SdkClientException, AmazonServiceException;
            val upload = transferManager!!.upload(por)
            //            XferMgrProgress.showTransferProgress(upload);
            showTransferProgress(upload)
            waitForCompletion(upload)
            //            // 设置此文件为公共读取
//            transferManager.getAmazonS3Client()
//                    .setObjectAcl(
//                        por.getBucketName(),
//                        por.getKey(),
//                        CannedAccessControlList.PublicRead
//                    );
        } catch (e: AmazonServiceException) {
            System.err.println("error file = " + por!!.key)
            System.err.println(e.toString())
        }
    }

    private fun showTransferProgress(upload: Upload) {
        println(upload.description)
        val progress = upload.progress
        do {
            try {
                Thread.sleep(100)
            } catch (e: InterruptedException) {
                return
            }
            val so_far = progress.bytesTransferred
            val total = progress.totalBytesToTransfer
            val pct = progress.percentTransferred
            print('\r')
            print("%n% 进度 $pct%")
        } while (!upload.isDone)
        // print the final state of the transfer.
        val xfer_state = upload.state
        println(" ")
        println("upload $xfer_state")
        when (xfer_state) {
            TransferState.Completed -> callback!!.complete()
            TransferState.Failed -> //                resetUpload();
                callback!!.failed()

            TransferState.InProgress -> {}
            TransferState.Canceled -> {}
            TransferState.Waiting -> {}
        }
    }
}
